﻿using Framework.Common;
using System;
using UnityEngine;

namespace BridgeShips {
    public class Value
    {
        private System.Action<int, int> m_ValueChangeEvent;
        private int m_LastValue;
        private int m_CurrentValue;
        public int CurrentValue
        {
            get
            {
                return m_CurrentValue;
            }
        }
        public int LastValue
        {
            get
            {
                return m_LastValue;
            }
        }

        public Value(int initValue)
        {
            m_LastValue = m_CurrentValue = initValue;
        }
        public bool Equals(Value value)
        {
            if (value == null) return false;
            return CurrentValue == value.CurrentValue;
        }
        public void SetValue(int value)
        {
            m_LastValue = m_CurrentValue;
            m_CurrentValue = value;
        }
        public void ChangeValue(int value)
        {
            m_LastValue = m_CurrentValue;
            m_CurrentValue += value;
            if (m_ValueChangeEvent != null)
                m_ValueChangeEvent(m_LastValue, m_CurrentValue);
        }
        public void AddValueChangeListener(Action<int, int> listener)
        {
            m_ValueChangeEvent += listener;
        }
        public void RemoveValueChangeListener(Action<int, int> listener)
        {
            m_ValueChangeEvent -= listener;
        }

        public void Release()
        {
            m_ValueChangeEvent = null;
        }
    }
}
